package com.leyou;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.sharding.entity.Order;
import com.leyou.sharding.mapper.OrderMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Random;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShardingSphereApplication.class)
public class Sharding {
    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Test
    public void testInsert() {
        Order order = new Order();
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            order = new Order();
            order.setUserId(1L);// 使用工具类获取拦截器传递过来的用户id
            order.setStatus(1); // 订单状态
            order.setSourceType(2); // 订单来源 1:app端，2：pc端，3：微信端
            order.setPostFee(0L);// 邮费：全场包邮
            order.setPaymentType(1);// 支付类型：在线支付
            order.setInvoiceType(0);// 发票类型，0无发票，1普通发票，2电子发票，3增值税发票
            order.setActualFee(1L);//实际支付 = 总金额 - 活动金额 ； 这里为了测试我们写个1分
            order.setTotalFee(Long.valueOf(random.nextInt(1000) + i));// 总金额
            orderMapper.insert(order);
        }

    }

    //查询order所有的记录
    @Test
    public void testFindALL() {
        List<Order> orders = orderMapper.selectList(null);
        System.out.println("orders = " + orders);
    }
    /**
     * 根据id查询
     */
    @Test
    public void findByIds(){

        Order order = orderMapper.selectById(1382891073193426946L);
        System.out.println("order = " + order);

    }

    /**
     * 根据其他字段查询
     */
    @Test
    public void  testFindStatus(){
        Order order = new Order();
        order.setStatus(2);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>(order);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        System.out.println("orderList = " + orderList);
    }
}
